<template>
  <div v-loading="loading" class="parentStyle">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('客户全称')" prop="name">
            <el-autocomplete
              ref="nameRef"
              v-model="form.name"
              class="inline-input name"
              :fetch-suggestions="companyQuerySearch"
              :placeholder="$t('公司全称')"
              :trigger-on-focus="false"
              :maxlength="50"
              show-word-limit
              @select="companyHandleSelect"
            >
              <template v-if="enableTyc" slot="append">
                <a @click="companyHandleSelect">{{ $t('查询工商信息') }}</a>
              </template>
            </el-autocomplete>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('客户简称')" prop="shortName">
            <el-input v-model="form.shortName" :placeholder="$t('客户简称')" show-word-limit maxlength="25" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('关注等级')" prop="followLevel">
            <dictSelect
              v-model="form.followLevel"
              :clearable="true"
              dictionary="czr_follow_level"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('负责人')" prop="manager">
            <UserSelect
              v-model="form.manager"
              :multiple="true"
              :placeholder="$t('负责人')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('客户分类')" prop="classify">
            <dictSelect
              v-model="form.classify"
              :clearable="true"
              dictionary="czr_classify"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('客户类型')" prop="type">
            <el-select v-model="form.type" :disabled="disabledType" :placeholder="$t('客户类型')">
              <el-option
                v-for="dict in typeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('联系电话')" prop="phone">
            <el-input
              v-model="form.phone"
              oninput="value=value.replace(/[^-\d.]/g,'')"
              :placeholder="$t('联系电话')"
              maxlength="20"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('客户邮箱')" prop="email">
            <el-input v-model="form.email" :placeholder="$t('客户邮箱')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('证件类型')">
            <el-input v-model="form.documentType" value="" :placeholder="$t('证件类型')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('证件号码')" prop="certificateId">
            <el-input v-model="form.certificateId" :placeholder="$t('证件号码')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('所属Group')" prop="affiliationGroup">
            <el-input v-model="form.affiliationGroup" :placeholder="$t('所属Group')" show-word-limit maxlength="32" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('机构法人')" prop="legalPerson">
            <el-input v-model="form.legalPerson" :placeholder="$t('机构法人')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('注册资本') + $t('金额单位')" prop="capitalRegistered">
            <money v-model="form.capitalRegistered" unit="w" :placeholder="$t('注册资本')" />
          </el-form-item>
        </el-col>
        <!--地域-->
        <Area ref="cityZj" :buss-data="form" />

        <el-col v-show="Isbirthday" :span="12">
          <el-form-item :label="$t('出生日期')" prop="birthday">
            <DatePicker
              v-model="form.birthday"
              :placeholder="$t('出生日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('联系地址')" prop="address">
            <el-input v-model="form.address" :placeholder="$t('联系地址')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('客户来源')" prop="customerSource">
            <el-input v-model="form.customerSource" :placeholder="$t('客户来源')" show-word-limit maxlength="250" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('推荐人')" prop="references">
            <el-input v-model="form.references" :placeholder="$t('推荐人')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资方向')" prop="investmentDirection">
            <el-input v-model="form.investmentDirection" :placeholder="$t('投资方向')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资阶段')" prop="investmentStage">
            <el-input v-model="form.investmentStage" :placeholder="$t('投资阶段')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('总体投资规模')" prop="overallInvestmentScale">
            <el-input v-model="form.overallInvestmentScale" :placeholder="$t('总体投资规模')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('单个项目投资规模')" prop="singleInvestmentScale">
            <el-input v-model="form.singleInvestmentScale" :placeholder="$t('单个项目投资规模')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('资金来源')" prop="capitalSource">
            <el-input v-model="form.capitalSource" :placeholder="$t('资金来源')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('GP在管规模要求')" prop="gpScaleRequirements">
            <el-input v-model="form.gpScaleRequirements" :placeholder="$t('GP在管规模要求')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('返投比例要求')" prop="proportionRequirements">
            <el-input v-model="form.proportionRequirements" :placeholder="$t('返投比例要求')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投委会席位要求')" prop="seatRequirements">
            <el-input v-model="form.seatRequirements" :placeholder="$t('投委会席位要求')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('审批流程')" prop="approvalProcess">
            <el-input v-model="form.approvalProcess" :placeholder="$t('审批流程')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('审批周期')" prop="approvalCycle">
            <el-input v-model="form.approvalCycle" :placeholder="$t('审批周期')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <!--
        <el-col :span="12">
          <el-form-item :label="$t('付款银行户名称')" prop="payBankAccountName">
            <el-input v-model="form.payBankAccountName" :placeholder="$t('付款银行户名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('付款银行户账号')" prop="payBankAccount">
            <el-input v-model="form.payBankAccount" :placeholder="$t('付款银行户账号')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('开户行')" prop="openBack">
            <el-input v-model="form.openBack" :placeholder="$t('开户行')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        -->
        <el-col :span="24">
          <el-form-item :label="$t('投资偏好')" prop="remark">
            <el-input
              v-model="form.preference"
              type="textarea"
              :placeholder="$t('投资偏好')"
              show-word-limit
              maxlength="2000"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('特别说明/合作诉求')" prop="specialNotes">
            <el-input
              v-model="form.specialNotes"
              type="textarea"
              :placeholder="$t('特别说明/合作诉求')"
              show-word-limit
              maxlength="2000"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('投资要求')" prop="requirement">
            <el-input
              v-model="form.requirement"
              type="textarea"
              :placeholder="$t('投资要求')"
              show-word-limit
              maxlength="2000"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('历史投资')" prop="remark">
            <el-input
              v-model="form.hisInvestment"
              type="textarea"
              :placeholder="$t('历史投资')"
              show-word-limit
              maxlength="2000"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>

      </el-row>

      <div class="area-header">
        <span class="area-header-title">{{ $t('联系人列表') }}</span>
      </div>
      <el-table
        :data="form.contactPersonList"
        border
        class="nowrapTable formTable"
      >
        <el-table-column :label="$t('序号')" align="center" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="150px">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('联系人名称') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'contactPersonList.' + scope.$index + '.contactsName'" :rules="contactPersonListRules.contactsName">
              <el-input v-model="scope.row.contactsName" maxlength="15" show-word-limit :placeholder="$t('联系人名称')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="150px">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('联系人类型') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'contactPersonList.' + scope.$index + '.personType'" :rules="contactPersonListRules.personType">
              <dict-select v-model="scope.row.personType" dictionary="contact_type" :placeholder="$t('联系人类型')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="150px">
          <template slot="header" slot-scope="{}">
            <span>{{ $t('职位') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'contactPersonList.' + scope.$index + '.position'" :rules="contactPersonListRules.position">
              <el-input v-model="scope.row.position" maxlength="15" show-word-limit :placeholder="$t('职位')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="150px">
          <template slot="header" slot-scope="{}">
            <span>{{ $t('邮箱') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'contactPersonList.' + scope.$index + '.email'" :rules="contactPersonListRules.email">
              <el-input v-model="scope.row.email" maxlength="100" show-word-limit :placeholder="$t('邮箱')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="150px">
          <template slot="header" slot-scope="{}">
            <span>{{ $t('电话') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'contactPersonList.' + scope.$index + '.phone'" :rules="contactPersonListRules.phone">
              <el-input v-model="scope.row.phone" oninput="value=value.replace(/[^-\d.]/g,'')" maxlength="25" show-word-limit :placeholder="$t('电话')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="150px">
          <template slot="header" slot-scope="{}">
            <span>{{ $t('备注') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'contactPersonList.' + scope.$index + '.remark'">
              <el-input v-model="scope.row.remark" maxlength="500" show-word-limit :placeholder="$t('备注')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column v-if="!otherBtnHide" :label="$t('操作')" align="center" width="60px">
          <template slot-scope="scope">
            <el-button v-hasPermi="['contact:person:remove']" size="small" type="text" icon="el-icon-delete" @click="delPersonHandle(scope.$index, scope.row)" />
          </template>
        </el-table-column>
      </el-table>
      <div v-if="!otherBtnHide" class="tol">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addPersonHandle">{{ $t("添加") }}</el-button>
      </div>

      <div class="area-header mt15">
        <span class="area-header-title">{{ $t('银行账户列表') }}</span>
      </div>
      <el-table
        :data="form.lpBaseBankAccountList"
        border
        class="nowrapTable formTable"
      >
        <el-table-column :label="$t('序号')" align="center" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="150px">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('付款银行户名称') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'lpBaseBankAccountList.' + scope.$index + '.bankName'" :rules="lpBaseBankAccountListRules.bankName">
              <el-input v-model="scope.row.bankName" :placeholder="$t('付款银行户名称')" maxlength="100" show-word-limit />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="150px">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('付款银行户账号') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'lpBaseBankAccountList.' + scope.$index + '.bankAccount'" :rules="lpBaseBankAccountListRules.bankAccount">
              <el-input v-model="scope.row.bankAccount" :placeholder="$t('付款银行户账号')" maxlength="100" show-word-limit />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="150px">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('开户行') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'lpBaseBankAccountList.' + scope.$index + '.bankAddress'" :rules="lpBaseBankAccountListRules.bankAddress">
              <el-input v-model="scope.row.bankAddress" :placeholder="$t('开户行')" maxlength="100" show-word-limit />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="150px">
          <template slot="header" slot-scope="{}">
            <span>{{ $t('备注') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'lpBaseBankAccountList.' + scope.$index + '.remark'">
              <el-input v-model="scope.row.remark" maxlength="500" show-word-limit :placeholder="$t('备注')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column v-if="!otherBtnHide" :label="$t('操作')" align="center" width="60px">
          <template slot-scope="scope">
            <el-button v-hasPermi="['lp:base:remove']" size="small" type="text" icon="el-icon-delete" @click="delHandle(scope.$index, scope.row)" />
          </template>
        </el-table-column>
      </el-table>
      <div v-if="!otherBtnHide" class="tol">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addHandle">{{ $t("添加") }}</el-button>
      </div>
      <FileTable :list.sync="form.fileList" data-type="lp_base_info" business-type="GR" :main-body-vue-show="false" :btn-show="!otherBtnHide" />
    </el-form>

    <!--保存，保存并启动流程，取消 三个按钮-->
    <div slot="footer" class="dialog-footer mt-15" style="text-align: right;">
      <SaveBtn has-permi="lp:base" :cancel-show="openLocation === undefined" />
    </div>
  </div>
</template>

<script>
import { checkCustomerName, checkLpName, getLpBase, saveLpBase } from '@/api/fund/lpBase/lpBase'
import { companyListByName, getCompanyByName } from '@/api/company/base/companyBase'
import { delDealContacts } from '@/api/deal/contact/contactsPerson'
import { Notification } from 'element-ui'
// 省市组件
import Area from '@/components/Area'

export default {
  components: {
    Area
  },
  data() {
    const checkLpNameValidator = (rule, value, callback) => {
      if (value === null || value === undefined) {
        return callback(new Error(this.$t('必填信息')))
      }
      checkLpName(value).then(response => {
        if (!response.data && !this.form.id) {
          callback(new Error(this.$t('客户已存在,不能重复添加!')))
        } else {
          callback()
        }
      })
    }
    // const checkPhoneAndEmail = (rule, value, callback) => {
    //   if (value !== undefined && value !== '' && value !== null) {
    //     // 校验邮箱
    //     for (var i = 0; i < this.form.contactPersonList.length; i++) {
    //       if (rule.field === 'contactPersonList.' + i + '.email') {
    //         var regEmail = /^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/
    //         if (!regEmail.test(value)) {
    //           callback(new Error(this.$t('无效邮箱')))
    //         }
    //       }
    //     }
    //     callback()
    //   } else {
    //     callback()
    //   }
    // }
    // var checkPhone = (rule, value, callback) => {
    //   if (value === '') {
    //     callback(new Error(this.$t('请输入联系方式')))
    //   } else {
    //     var mobile = /^1(3|4|5|6|7|8|9)\d{9}$/
    //     if (!mobile.test(value)) {
    //       callback(new Error(this.$t('格式不正确')))
    //     }
    //     callback()
    //   }
    // }
    return {
      // 表单参数
      form: {
        lpBaseBankAccountList: [],
        contactPersonList: []
      },
      // 遮罩层
      loading: false,
      Isbirthday: false,
      otherBtnHide: false,
      openLocation: undefined,
      // 表单校验
      rules: {
        name: [{ required: true, validator: checkLpNameValidator, trigger: ['blur'] }],
        type: [{ required: true, message: this.$t('必填信息'), trigger: ['blur'] }],
        shortName: [{ required: true, message: this.$t('必填信息'), trigger: ['blur'] }],
        email: [{ required: false, type: 'email', message: this.$t('邮箱格式不正确') }],
        classify: [{ required: true, message: this.$t('必填信息'), trigger: ['change', 'blur'] }],
        // phone: [{ required: true, validator: checkPhone, trigger: ['blur'] }],
        manager: [{ required: true, message: this.$t('必填信息'), trigger: ['blur'] }],
        followLevel: [{ required: true, message: this.$t('必填信息'), trigger: ['blur'] }]
      },
      lpBaseBankAccountListRules: {
        bankName: [{ required: true, message: this.$t('必填信息'), trigger: ['blur'] }],
        bankAccount: [{ required: true, message: this.$t('必填信息'), trigger: ['blur'] }],
        bankAddress: [{ required: true, message: this.$t('必填信息'), trigger: ['blur'] }]
      },
      contactPersonListRules: {
        contactsName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        personType: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        // email: [{ required: false, validator: checkPhoneAndEmail,	trigger: 'blur' }],
        // phone: [{ required: false, validator: checkPhoneAndEmail,	trigger: 'blur' }]
        email: [{ required: false, type: 'email', message: this.$t('邮箱格式不正确') }]
      },
      // 客户类型字典
      typeOptions: [],
      // 客户级别字典
      levelOptions: [],
      // 客户分类
      classifys: [],
      // 天眼查查询条件
      tycParam: {
        id: '',
        companyName: '',
        tycId: ''
      },
      // 查询是否存在条件
      existParam: {
        name: '',
        id: ''
      },
      disabledType: false,
      enableTyc: undefined
    }
  },
  watch: {
    'form.type': function(val) {
      console.log('监听')
      // this.fullName = val + ' ' + this.lastName
      this.changType(val)
    },
    'form.classify': function(val) {
      console.log('监听')
      // this.fullName = val + ' ' + this.lastName
      this.changClassify(val)
    }
  },
  created() {
    this.getDicts('lp_type').then(response => {
      this.typeOptions = response.data
    })
    this.getDicts('lp_level').then(response => {
      this.levelOptions = response.data
    })
    this.getDicts('czr_classify').then(response => {
      this.classifys = response.data
    })
  },
  mounted() {
    this.$bus.$emit('async_done')
    this.enableTyc = (window.localStorage.getItem('enableTyc') === 'true')
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getLpBase(data.id).then(response => {
          this.form = response.data
          this.tycParam.companyName = this.form.name
          // 导入时注册地-市数据回显
          this.$nextTick(() => {
            if (this.$refs?.cityZj?.initCity !== undefined) {
                  this.$refs.cityZj.initCity()
                  this.$refs.cityZj.initDistrict()
            }
          })
          this.loading = false
        })
      } else {
        this.reset()
        // 默认负责人等于登录用户
        this.form.manager = [this.$store.state.user.userId]
        this.disabledType = false
        if (data && data.type) {
          this.form.type = data.type
          this.disabledType = true
        }
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        shortName: undefined,
        phone: undefined,
        affiliationGroup: undefined,
        email: undefined,
        type: undefined,
        level: undefined,
        documentType: undefined,
        certificateId: undefined,
        address: undefined,
        payBankAccountName: undefined,
        payBankAccount: undefined,
        openBack: undefined,
        branch: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        creditCode: undefined,
        legalPerson: undefined,
        setDate: undefined,
        industryFirst: undefined,
        industrySecond: undefined,
        provinceRegistered: undefined,
        cityRegistered: undefined,
        districtRegistered: undefined,
        detailsRegistered: undefined,
        capitalRegistered: undefined,
        currency: undefined,
        tycId: undefined,
        logoInfo: undefined,
        companyId: undefined,
        requirement: undefined,
        specialNotes: undefined,
        customerSource: undefined,
        birthday: undefined,
        manager: undefined,
        followLevel: undefined,
        preference: undefined,
        hisInvestment: undefined,
        investmentDirection: undefined,
        investmentStage: undefined,
        overallInvestmentScale: undefined,
        singleInvestmentScale: undefined,
        capitalSource: undefined,
        gpScaleRequirements: undefined,
        proportionRequirements: undefined,
        seatRequirements: undefined,
        approvalProcess: undefined,
        approvalCycle: undefined
      }
      this.resetForm('form')
    },
    delHandle(index) {
      this.form.lpBaseBankAccountList.splice(index, 1)
    },
    delPersonHandle(index, row) {
      const id = row.id
      if (id !== undefined && id !== '' && id != null) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        }).then(function() {
          return delDealContacts(id)
        }).then(() => {
          this.form.contactPersonList.splice(index, 1)
          return this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {})
      } else {
        this.form.contactPersonList.splice(index, 1)
      }
    },
    addHandle() {
      const row = {
        id: undefined,
        bankName: undefined,
        bankAccount: undefined,
        bankAddress: undefined
      }
      if (!(this.form.lpBaseBankAccountList instanceof Array)) {
        this.$set(this.form, 'lpBaseBankAccountList', [])
      }
      this.$nextTick(() => {
        this.form.lpBaseBankAccountList.push(row)
      })
    },
    addPersonHandle() {
      const row = {
        id: undefined,
        businessId: undefined,
        businessType: 'lpInvestor',
        contactsName: undefined,
        personType: undefined,
        position: undefined,
        email: undefined,
        phone: undefined
      }
      if (!(this.form.contactPersonList instanceof Array)) {
        this.$set(this.form, 'contactPersonList', [])
      }
      this.$nextTick(() => {
        this.form.contactPersonList.push(row)
      })
    },
    // 取消按钮
    cancel() {
      if (this.$parent.$parent.lpBaseFormOpen !== undefined) {
        this.$parent.$parent.lpBaseFormOpen = false
      } else {
        this.$parent.$parent.formOpen = false
        this.$parent.$parent.reset()
      }
    },
    // 关闭form页面
    closeForm() {
      if (this.$parent.$parent.lpBaseFormOpen !== undefined) {
        this.$parent.$parent.lpBaseFormOpen = false
      } else {
        // 关闭form页面遮罩层
        this.loading = false
        // 关闭form页面
        this.$parent.$parent.formOpen = false
        // 刷新list页面
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.phone !== '' && this.form.phone !== undefined && this.form.phone !== null) {
            var mobile = /^1(3|4|5|6|7|8|9)\d{9}$/
            if (!mobile.test(this.form.phone)) {
              this.msgError(this.$t('电话格式不正确'))
              return
            }
          }
          this.loading = true
          saveLpBase(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.$emit('senddata', response.data)
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    // 标的公司查询
    companyQuerySearch(queryString, cb) {
      companyListByName(queryString).then(res => {
        if (res.code === 200) {
          cb(res.data)
        }
      })
    },
    // 标的公司选择后事件
    companyHandleSelect(item) {
      this.loading = true
      const name = this.form.name
      if (name === '' || name === undefined) {
        Notification.error({
          title: this.$t('请先输入客户全称！')
        })
        this.loading = false
        return
      }
      this.existParam.name = name
      this.existParam.id = this.form.id
      checkCustomerName(this.existParam).then(response => {
        if (response.data) {
          this.form.name = ''

          this.msgError(this.$t('该用户全称已存在!'))
          this.loading = false
        } else {
          this.tycParam.tycId = item.id
          this.tycParam.companyName = name
          getCompanyByName(this.tycParam).then(res => {
            if (res.code === 200) {
              if (res.msg === 'nofind') {
                this.form.name = undefined
                Notification.error({
                  title: this.$t('天眼查没有获取到该客户信息！')
                })
                this.loading = false
              } else {
                // 公司简称
                this.form.name = name
                // 客户简称
                this.form.shortName = name
                // 企业统一社会信用代码
                this.form.certificateId = res.data.creditCode
                // 法人代表
                this.form.legalPerson = res.data.legalPerson
                // 成立时间
                this.form.establishTime = res.data.establishTime
                // 注册地-省/市
                this.form.provinceRegistered = res.data.provinceRegistered
                this.form.cityRegistered = res.data.cityRegistered
                this.$refs.cityZj.initCity()
                this.$refs.cityZj.initDistrict()
                  // 注册区
                this.form.districtRegistered = res.data.districtRegistered
                // 注册地-详细地址
                this.form.detailsRegistered = res.data.detailsRegistered
                // 注册资本金额
                this.form.capitalRegistered = res.data.capitalRegistered
                // 注册资本币种
                this.form.currency = res.data.currency
                // 天眼查ID
                this.form.tycId = res.data.tycId
                // logo
                this.form.logoInfo = res.data.logoInfo
                // 关联公司ID
                this.form.companyId = res.data.id
                this.msgSuccess(this.$t('已获取工商信息！'))
                this.loading = false
              }
            }
          })
        }
      })
    },
    // 初始化天眼查获取的数据
    resetForm() {
      this.form.name = undefined
      this.form.shortName = undefined
      this.form.creditCode = undefined
      this.form.legalPerson = undefined
      this.form.setDate = undefined
      this.form.industryFirst = undefined
      this.form.industrySecond = undefined
      this.form.provinceRegistered = undefined
      this.form.cityRegistered = undefined
      this.form.districtRegistered = undefined
      this.form.detailsRegistered = undefined
      this.form.capitalRegistered = undefined
      this.form.currency = undefined
      this.form.tycId = undefined
      this.form.logoInfo = undefined
      this.form.companyId = undefined
    },
    changClassify(val) {
      if (!this.form.type) {
        for (let i = 0; i < this.classifys.length; i++) {
          const dict = this.classifys[i]
          if (dict.dictValue === val) {
            if (dict.remark === 'gr') {
              this.$set(this.form, 'type', 'GR')
              return
            }
          }
        }
        this.$set(this.form, 'type', 'JG')
      }
    },
    changType(val) {
      console.log(val)
      if (val === 'JG') {
        this.Isbirthday = false
      } else {
        this.Isbirthday = true
      }
    }
  }

}
</script>
